#include "delewidget.h"
#include <QDebug>

#define _QPushButton_black1 "QPushButton{background-color :rgb(180,180,240); color:rgb(0, 0, 255);\
border:0.3px solid white;border-radius: 3px;}QPushButton:pressed { background-color: rgb(160,160,200); }"

ItemButton::ItemButton(QString iconName): buttonWidget(new QPushButton())
{
    m_buttonIconName=iconName;
     buttonWidget->setStyleSheet(_QPushButton_black1);
}

void ItemButton::paintButton(QPainter* painter, const QStyleOptionViewItem& option, const QPoint& pos)
{
    QStyleOptionButton buttonOption;
    //option可以获取表格的坐标和宽高
   // qDebug()<<"paintButton";

    buttonOption.rect = QRect(option.rect.x()+left, option.rect.y()+top, option.rect.width()-right, option.rect.height()-button);
    buttonOption.state = QStyle::State_Enabled;
    if(buttonOption.rect.contains(pos))
    {
        if(buttonStatus == BUTTON_STATUS::HOVER)
            buttonOption.state |= QStyle::State_MouseOver;
        else if(buttonStatus == BUTTON_STATUS::PRESSED)
            buttonOption.state |= QStyle::State_Sunken;
    }
    //QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, buttonWidget.data());
    //option.widget->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, buttonWidget.data());
    buttonWidget.data()->style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter, buttonWidget.data());
}

void ItemButton::setStatusNone()
{
    buttonStatus = NONE;
}

void ItemButton::setStatusHover()
{
    buttonStatus = HOVER;
}

void ItemButton::setStatusPressed()
{
    buttonStatus = PRESSED;
}

void ItemButton::setStatusReleased()
{
    buttonStatus = RELEASED;
}

bool ItemButton::withinButtonRegion(const QPoint pos, const QStyleOptionViewItem& option)
{
    QStyleOptionButton buttonOption;
    buttonOption.rect = QRect(option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height());
    return buttonOption.rect.contains(pos);
}

ItemProgressBar::ItemProgressBar(): progressBarWidget(new QProgressBar())
{
    progressBarWidget->setStyleSheet("\
                                        QProgressBar{\
                                            border: 2px solid #347;\
                                            border-radius:4px;\
                                        }\
                                        QProgressBar::chunk {\
                                            background-color: #77AAEE;\
                                        }\
                                     ");
}

void ItemProgressBar::paintQProgressBar(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index)
{

    int progress = index.data().toDouble() * 100;

    QStyleOptionProgressBar progressBarOption;
    progressBarOption.initFrom(option.widget);
    progressBarOption.rect = QRect(option.rect.x() + left, option.rect.y() + top, option.rect.width() - left - right, option.rect.height() - top - bottom);
    progressBarOption.minimum = 0;
    progressBarOption.maximum = 100;
    progressBarOption.progress = progress;
    progressBarOption.text = QString::number(progress) + "%";
    progressBarOption.textVisible = true;
    progressBarOption.textAlignment = Qt::AlignHCenter | Qt::AlignVCenter;

    progressBarWidget.data()->style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter, progressBarWidget.data());

}

